<?php

namespace Article;

use C\L\AdmController;

class ArtController extends AdmController
{
    protected function init()
    {
        $this->service = $this->s_article;
        $this->likeSearchKeys = [
        'title',
        ];

        $this->pubSearchKeys = [
        'cat'
        ];

        $this->hideKeys = [
        'is_delete'
        ];

        $this->timeToDateKeys = [
        'uptime', 'addtime'
        ];

        $this->updateKeys = [
        'title', 'content', 'is_disable', 'sort', 'cat'
        ];

        $this->createKeys = [
        'title', 'content', 'is_disable', 'sort', 'cat'
        ];
    }

    protected function beforeSearch()
    {
        $this->params['order'] = 'sort desc';
        return true;
    }

    protected function afterSearch(&$data)
    {
        foreach ($data['list'] as &$item) {
            $item['url'] = '/art/' . $item['code'];
        }
        return true;
    }

    protected function beforeUpdate(&$data)
    {
        if (empty($data['cat'])) {
            $this->error('必须选择分类');
        }
        $data['content'] = $this->request->getPost('content');
        return true;
    }

    protected function beforeCreate(&$data)
    {
        if (empty($data['cat'])) {
            $this->error('必须选择分类');
        }
        $data['content'] = $this->request->getPost('content');
        return true;
    }

    protected function afterCreate(&$data)
    {
        $this->s_article->update($data['id'], ['code' => substr(md5($data['id']), 0, 6)]);
        return true;
    }
}
